package com.cskaoyan.controller.lz_controller;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.lzpackage.lz_wx.*;
import com.cskaoyan.mapper.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("wwx")
public class WxCartController {
    @Autowired
    CategoryMapper categoryMapper;
//    @RequestMapping("catalog/index")
//    public BaseRespVo list() {
//        //获得UserId
//        Subject subject = SecurityUtils.getSubject();
//        Integer id = (Integer) subject.getPrincipals().getPrimaryPrincipal();
//        WxIndexData wxIndexData=new WxIndexData();
//        CategoryExample categoryExample=new CategoryExample();
//        CategoryExample.Criteria criteria = categoryExample.createCriteria();
//        CategoryExample.Criteria criteria1 = criteria.andPidEqualTo(0);
//        List<Category> categories = categoryMapper.selectByExample(categoryExample);
//
//        Category category = categories.get(0);
//        Integer currentcategoryId = category.getId();
//        wxIndexData.setCurrentCategory(category);
//        wxIndexData.setCategoryList(categories);
//
//        CategoryExample categoryExample1=new CategoryExample();
//        CategoryExample.Criteria criteria2 = categoryExample1.createCriteria();
//        CategoryExample.Criteria criteria3 = criteria2.andPidEqualTo(currentcategoryId);
//        List<Category> categories1 = categoryMapper.selectByExample(categoryExample1);
//        wxIndexData.setCurrentSubCategory(categories1);
//        return BaseRespVo.ok(wxIndexData);
//    }

@Autowired
    CartMapper cartMapper;
    @Autowired
    GoodsMapper goodsMapper;


    @RequestMapping("cart/index")
    public BaseRespVo cartIndex() {
        CartData cartData=new CartData();
        CartExample cartExample = new CartExample();



        List<Cart> carts = cartMapper.selectByExample(cartExample);
        cartData.setCartList(carts);



        long cartcount = cartMapper.countByExample(cartExample);
        CartExample.Criteria criteria = cartExample.createCriteria();
        criteria.andCheckedEqualTo(true);
        long checkedcount = cartMapper.countByExample(cartExample);


        CartExample amountExample = new CartExample();
        CartExample.Criteria amountExampleCriteria = amountExample.createCriteria();
             Double[] priceAll= cartMapper.selectPriceAll();
             Double goodsAmount=0.0;
        for (Double price:priceAll
             ) {
            goodsAmount+=price;
        }
        CartTotal cartTotal=new CartTotal();
        cartTotal.setGoodsAmount(goodsAmount);


        Double goodsCheckedAmount=0.0;
        Double[] priceChecked= cartMapper.selectPriceChecked();
        for (Double price:priceChecked
        ) {
            goodsCheckedAmount+=price;
        }

        cartTotal.setCheckedGoodsAmount(goodsCheckedAmount);
        cartTotal.setCheckedGoodsCount((double) checkedcount);
        cartTotal.setGoodsCount((double) cartcount);

        cartData.setCartTotal(cartTotal);
        return BaseRespVo.ok(cartData);
    }







    @RequestMapping("cart/checked")
    public BaseRespVo checked(@RequestBody ProductData productIds) {
        Integer[] productIds1 = productIds.getProductIds();
        Boolean ischecked = productIds.getIsChecked();
        if (productIds1.length==1){
               int id =   productIds1[0];
            Boolean bo= cartMapper.selectChecked(id);
            int i= cartMapper.updatecheckedById(id,bo);
        }
        else {
            for (Integer id:productIds1
            ) {
                Boolean bo=  cartMapper.selectChecked(id);
                int i= cartMapper.updatecheckedByIdGroup(id,ischecked);
            }
        }




        CartData cartData=new CartData();
        CartExample cartExample = new CartExample();



        List<Cart> carts = cartMapper.selectByExample(cartExample);
        cartData.setCartList(carts);



        long cartcount = cartMapper.countByExample(cartExample);
        CartExample.Criteria criteria = cartExample.createCriteria();
        criteria.andCheckedEqualTo(true);
        long checkedcount = cartMapper.countByExample(cartExample);


        CartExample amountExample = new CartExample();
        CartExample.Criteria amountExampleCriteria = amountExample.createCriteria();
        Double[] priceAll= cartMapper.selectPriceAll();
        Double goodsAmount=0.0;
        for (Double price:priceAll
        ) {
            goodsAmount+=price;
        }
        CartTotal cartTotal=new CartTotal();
        cartTotal.setGoodsAmount(goodsAmount);


        Double goodsCheckedAmount=0.0;
        Double[] priceChecked= cartMapper.selectPriceChecked();
        for (Double price:priceChecked
        ) {
            goodsCheckedAmount+=price;
        }

        cartTotal.setCheckedGoodsAmount(goodsCheckedAmount);
        cartTotal.setCheckedGoodsCount((double) checkedcount);
        cartTotal.setGoodsCount((double) cartcount);

        cartData.setCartTotal(cartTotal);
        return BaseRespVo.ok(cartData);
    }



    @RequestMapping("cart/update")
    public BaseRespVo update(@RequestBody UpdateData updateData) {
      int i= cartMapper.updateNumberById(updateData.getId(),updateData.getNumber());
      return BaseRespVo.out();
    }

    @RequestMapping("cart/delete")
    public BaseRespVo delete(@RequestBody  DeleteData productIds ) {
        Integer[] productIds1 = productIds.getProductIds();
        for (Integer id: productIds1
             ) {
            int i = cartMapper.deleteByPid(id);
        }






        CartData cartData=new CartData();
        CartExample cartExample = new CartExample();



        List<Cart> carts = cartMapper.selectByExample(cartExample);
        cartData.setCartList(carts);



        long cartcount = cartMapper.countByExample(cartExample);
        CartExample.Criteria criteria = cartExample.createCriteria();
        criteria.andCheckedEqualTo(true);
        long checkedcount = cartMapper.countByExample(cartExample);


        CartExample amountExample = new CartExample();
        CartExample.Criteria amountExampleCriteria = amountExample.createCriteria();
        Double[] priceAll= cartMapper.selectPriceAll();
        Double goodsAmount=0.0;
        for (Double price:priceAll
        ) {
            goodsAmount+=price;
        }
        CartTotal cartTotal=new CartTotal();
        cartTotal.setGoodsAmount(goodsAmount);


        Double goodsCheckedAmount=0.0;
        Double[] priceChecked= cartMapper.selectPriceChecked();
        for (Double price:priceChecked
        ) {
            goodsCheckedAmount+=price;
        }

        cartTotal.setCheckedGoodsAmount(goodsCheckedAmount);
        cartTotal.setCheckedGoodsCount((double) checkedcount);
        cartTotal.setGoodsCount((double) cartcount);

        cartData.setCartTotal(cartTotal);
        return BaseRespVo.ok(cartData);
    }


@Autowired
    GoodsProductMapper goodsProductMapper;
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    OrderGoodsMapper orderGoodsMapper;
/*
    Integer cartId,Integer addressId,Integer couponId,Integer userCouponId, Integer grouponRulesId
*/
    @RequestMapping("cart/checkout")
    public BaseRespVo checkout() {


        CheckOutData checkOutData=new CheckOutData();
        // 查询actualPrice
        CartExample amountExample = new CartExample();
        CartExample.Criteria amountExampleCriteria = amountExample.createCriteria();
        Double[] priceAll= cartMapper.selectPriceAll();
        Double goodsAmount=0.0;
        for (Double price:priceAll
        ) {
            goodsAmount+=price;
        }
        checkOutData.setGoodsTotalPrice(goodsAmount);
        // 查询List<GoodsProduct>
       CartExample cartExample=new CartExample();
        CartExample.Criteria criteria1 = cartExample.createCriteria();
        criteria1.andCheckedEqualTo(true);
        List<Cart> carts = cartMapper.selectByExample(cartExample);
        checkOutData.setCheckedGoodsList(carts);

        //查询 isdefault 为 ture 的LIST<Address>
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andIsDefaultEqualTo(true);
        List<Address> addresses = addressMapper.selectByExample(addressExample);
       checkOutData.setCheckedAddress(addresses);
         //设置AddressId
        Integer addressid = addresses.get(0).getId();
        checkOutData.setAddressId(addressid);
        //设置CartId
    /*    goodsProductMapper.selectByExample()
        Integer cartid = goodsProducts.get(0).getId();*/
        checkOutData.setCartId(0);

        List<Double> list=couponMapper.selectByprice();
        Double discount = list.get(0);
        checkOutData.setCouponPrice(discount);

      List<Integer> idlist=  couponMapper.selectIdByPrice();
        Integer couponid = idlist.get(0);
        checkOutData.setCouponId(couponid);

      Double freightprice= orderGoodsMapper.selectFreightPrice();
      if (freightprice==null){
          freightprice=0.0;
      }
      checkOutData.setFreightPrice(freightprice);
        Double grouponprice= orderGoodsMapper.selectGroupOnPrice();
        checkOutData.setGrouponPrice(grouponprice);
      Integer rulesid=  orderGoodsMapper.selectgrouponRulesId();
      checkOutData.setGrouponRulesId(rulesid);

      Double ordertotalprice =goodsAmount-discount;
      if (ordertotalprice<=0){
          ordertotalprice=0.0;
      }
      checkOutData.setActualPrice(ordertotalprice);
      checkOutData.setOrderTotalPrice(ordertotalprice);

      checkOutData.setUserCouponId(0);

      return BaseRespVo.ok(checkOutData);
     }



    @RequestMapping("cart/add")
    public Integer add(@RequestBody FastAddData fastAddData){
        Integer goodsId = fastAddData.getGoodsId();
        Short number = fastAddData.getNumber();
        Integer productId = fastAddData.getProductId();
        GoodsProduct goodsProduct = goodsProductMapper.selectByPrimaryKey(productId);
        Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
        Cart cart= new Cart();
        cart.setProductId(productId);
        cart.setGoodsSn(goods.getGoodsSn());
        cart.setPrice(goods.getRetailPrice());
        cart.setGoodsName(goods.getName());
        cart.setGoodsId(goods.getId());
        Date date=new Date();
        cart.setAddTime(date);
        cart.setPicUrl(goods.getPicUrl());

        if ( cartMapper.selectByproductId(productId)!=null){
            Short number1 = cartMapper.selectByproductId(productId).getNumber();
            Integer id = cartMapper.selectByproductId(productId).getId();
            cartMapper.updateNumberById(id,number1.intValue());
        }else {
            cartMapper.insertSelective(cart);
        }


        int i = cartMapper.insertSelective(cart);
        return i;
    }



    @RequestMapping("cart/goodscount")
    public BaseRespVo goodscount(){
        CartExample cartExample=new CartExample();
        long cartcount = cartMapper.countByExample(cartExample);
        return BaseRespVo.ok(cartcount);
    }

    @RequestMapping("cart/fastadd")
    public BaseRespVo fastadd(@RequestBody FastAddData fastAddData){
        Goods goods = goodsMapper.selectByPrimaryKey(fastAddData.getGoodsId());
        Cart cart=new Cart();
        cart.setGoodsId(goods.getId());
        cart.setNumber(fastAddData.getNumber());
        cart.setProductId(fastAddData.getProductId());
        cart.setGoodsName(goods.getName());
        cart.setGoodsSn(goods.getGoodsSn());
        cart.setPrice(goods.getRetailPrice());
        Date date=new Date();
        cart.setAddTime(date);
         cart.setChecked(false);
        int i = cartMapper.insertSelective(cart);
        Integer cartId = cart.getId();
        return  BaseRespVo.ok(cartId);
    }
@Autowired
AdMapper adMapper;
@Autowired
BrandMapper brandMapper;
@Autowired
GrouponMapper grouponMapper;
@Autowired
TopicMapper topicMapper;

/*    List<Ad>  banner;
    List<Brand> brandList;
    List<IndexChannel> channel;
    List<Coupon> couponList;
    List<FloorGoodsList> floorGoodsList;
    List<Groupon> grouponList;
    List<Goods> hotGoodsList;
    List<Goods> newGoodsList;
    List<Topic> topicList;*/

    @RequestMapping("home/index")
    public BaseRespVo index(){
        IndexData indexDat=new IndexData();
        //setBanner
        AdExample adExample=new AdExample();
        List<Ad> ads = adMapper.selectByExample(adExample);
        indexDat.setBanner(ads);

        //setBrandList
        BrandExample brandExample=new BrandExample();
        brandExample.setOrderByClause("id desc "+"limit 6");
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        indexDat.setBrandList(brands);

        //setChannel
       List<IndexChannel> channel= categoryMapper.selectIndexData();
       indexDat.setChannel(channel);

       //setCouponList
       CouponExample couponExample=new CouponExample();
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);
        indexDat.setCouponList(coupons);

        //setGrouponList
        GrouponExample grouponExample=new GrouponExample();
        List<Groupon> groupons = grouponMapper.selectByExample(grouponExample);
        indexDat.setGrouponList(groupons);

        //setHotGoodsList
        GoodsExample goodsExample=new GoodsExample();
        GoodsExample.Criteria goodsExampleCriteria = goodsExample.createCriteria();
        goodsExampleCriteria.andIsHotEqualTo(true);
        goodsExample.setOrderByClause("id","desc"+" limit 4");
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);
        indexDat.setHotGoodsList(goods);

        //setNewGoodsList
        GoodsExample goodsExample1=new GoodsExample();
        GoodsExample.Criteria goodsExample1Criteria = goodsExample1.createCriteria();
        goodsExample1Criteria.andIsNewEqualTo(true);
        goodsExample1.setOrderByClause("id","desc"+" limit 4");
        List<Goods> goods1 = goodsMapper.selectByExample(goodsExample1);
        indexDat.setNewGoodsList(goods1);

        //setTopicList
        TopicExample topicExample=new TopicExample();
        topicExample.setOrderByClause("id desc"+" limit 3");
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        indexDat.setTopicList(topics);
        return BaseRespVo.ok(indexDat);
    }


 /*   @RequestMapping("cart/goodscount")
    public BaseRespVo goodscount(){
        CartExample cartExample=new CartExample();
        long cartcount = cartMapper.countByExample(cartExample);
        return BaseRespVo.ok(cartcount);
    }*/
}
